package com.lishem.carl._03hasbtable;

/**
 * https://leetcode.cn/problems/ransom-note/description/
 * <p>
 * 给你两个字符串：ransomNote和magazine ，判断ransomNote能不能由magazine里面的字符构成。
 * <p>
 * 如果可以，返回true ；否则返回 false 。
 * <p>
 * magazine中的每个字符只能在ransomNote中使用一次。
 * <p>
 * 示例 1：
 * <p>
 * 输入：ransomNote = "a", magazine = "b"
 * <p>
 * 输出：false
 * <p>
 * 示例 2：
 * <p>
 * 输入：ransomNote = "aa", magazine = "ab"
 * <p>
 * 输出：false
 * <p>
 * 示例 3：
 * <p>
 * 输入：ransomNote = "aa", magazine = "aab"
 * <p>
 * 输出：true
 */
public class _6LetCode383_赎金信 {

    public boolean canConstruct(String ransomNote, String magazine) {
        int[] count = new int[26];
        char[] chars = magazine.toCharArray();
        for (char aChar : chars) {
            count[aChar - 'a']++;
        }
        chars = ransomNote.toCharArray();
        for (char aChar : chars) {
            if (--count[aChar - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        _6LetCode383_赎金信 sol = new _6LetCode383_赎金信();
        System.out.println(sol.canConstruct("a", "b")); // false
        System.out.println(sol.canConstruct("aa", "ab")); // false
        System.out.println(sol.canConstruct("aa", "aab")); // true
    }

}
